package org.mte.sak.sort.comparator;

import java.util.HashMap;

import org.mte.sak.sort.comparator.impl.*;
import org.mte.sak.util.ClassType;

/**
 * Gestisce l'utilizzo delle classi di tipo <i>comparatori</i>
 * secondo il tipo di dati per realizzare l'ordinamento.
 *
 * @author Marco Traversari
 * @version $Revision: 1.2 $ $Date: 2011-05-04 18:28:42 $
 *
 */
public class ComparatorFactory {

//    /**
//     * PACKAGE_NAME
//     */
//    private static final String PACKAGE_NAME = "org.dromedary.lib.sort";

	/**
	 * Definizione Comparatori
	 */
	 private static HashMap<String, String> hmComparatorsDefs = null;

     /**
      * Comparatori
      */
     private static HashMap<String, Comparator> hmComparators = null;

	 /**
	  * createComparators()
	  */
     private static void createComparators() {
         hmComparatorsDefs = new HashMap<String, String>();
         hmComparatorsDefs.put(ClassType.STRING.def, StringComparator.class.getName());
         hmComparatorsDefs.put(ClassType.INTEGER.def, IntegerComparator.class.getName());
         hmComparatorsDefs.put(ClassType.INT.def, IntegerComparator.class.getName());
         hmComparatorsDefs.put(ClassType.DOUBLE.def, DoubleComparator.class.getName());
         hmComparatorsDefs.put(ClassType.DOUBLE_OBJ.def, DoubleComparator.class.getName());
         hmComparatorsDefs.put(ClassType.DATE.def, DateComparator.class.getName());
         hmComparatorsDefs.put(ClassType.DATE_SQL.def, DateComparator.class.getName());
         hmComparatorsDefs.put(ClassType.LONG.def, DoubleComparator.class.getName());
		 hmComparatorsDefs.put(ClassType.FILE_OBJECT.def,StringComparator.class.getName());
		 hmComparatorsDefs.put(ClassType.BIG_DECIMAL.def, BigDecimalComparator.class.getName());
     }

	/**
	 * Restituisce un comparatore secondo il tipo classe specificato.
	 *
	 * @param classType per cui si richede un comparatore.
	 * @return un oggetto <code>Comparator</code>.
	 */
	public static Comparator getComparator(String classType) {
		Comparator comparator = null;
        try {
		    if (hmComparators == null) {
                hmComparators = new HashMap<String, Comparator>();
            }
            comparator = hmComparators.get(classType);

            if (comparator == null) {
                if (hmComparatorsDefs == null) {
                    createComparators();
                }
                String strComparator = hmComparatorsDefs.get(classType);
                comparator = (Comparator) Class.forName(strComparator).newInstance();
                hmComparators.put(classType, comparator);
            }

        } catch (Exception e) {
			e.printStackTrace();
		}
		return comparator;
	}
}